package la.bbs558.controller.user;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import la.bbs558.aop.ratelimit.LimitType;
import la.bbs558.aop.ratelimit.RateLimiter;
import la.bbs558.aop.valid.Check;
import la.bbs558.aop.valid.Valid;
import la.bbs558.controller.base.BaseController;
import la.bbs558.controller.base.Result;
import la.bbs558.entity.vo.BbsPostVO;
import la.bbs558.entity.vo.BbsReplyVO;
import la.bbs558.entity.vo.InvitationCodeVO;
import la.bbs558.entity.enums.OssFolder;
import la.bbs558.entity.enums.UserSex;
import la.bbs558.entity.params.UserCenterSearchPage;
import la.bbs558.entity.params.UserUpdateParams;
import la.bbs558.service.InvitationCodeService;
import la.bbs558.service.UserCenterService;
import la.bbs558.util.OssUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Classname UserController
 * @Description 用户中心
 * @Date 2023/12/15 10:27
 * @Created by weihanmin
 */
@RestController
@RequestMapping("user/center")
@Check
@RequiredArgsConstructor
public class UserCenterController extends BaseController {
    private final UserCenterService userCenterService;
    private final InvitationCodeService invitationCodeService;
    private final OssUtil ossUtil;

    @GetMapping("statistics")
    @SaIgnore
    public Result<?> userStatistics(@RequestParam(defaultValue = "") Long userId) {
        return Result.success(userCenterService.getUserStatistics(userId));
    }

    @GetMapping("post")
    @SaIgnore
    public Result<?> userPost(UserCenterSearchPage<BbsPostVO> searchPage) {
        userCenterService.getUserPost(searchPage);
        return Result.success(searchPage);
    }

    @GetMapping("reply")
    @SaIgnore
    public Result<?> userReply(UserCenterSearchPage<BbsReplyVO> searchPage) {
        userCenterService.getUserReply(searchPage);
        return Result.success(searchPage);
    }

    @GetMapping("sexList")
    @SaIgnore
    public Result<?> getSexList() {
        return Result.success(UserSex.getStateList());
    }

    @GetMapping("info")
    public Result<?> info() {
        return Result.success(userCenterService.getUserInfo(StpUtil.getLoginIdAsLong()));
    }

    @PostMapping("uploadAvatar")
    @RateLimiter(key = "updateAvatar:",time = 1,count = 1, limitType = LimitType.USER_ID)
    public Result<?> updateAvatar(MultipartFile file) {
        String key = ossUtil.uploadFile(file, OssFolder.USER, StpUtil.getLoginIdAsString());
        userCenterService.updateAvatar(key);
        return Result.success(key);
    }

    @PostMapping("updateInfo")
    @RateLimiter(key = "updateInfo:",time = 1,count = 1, limitType = LimitType.USER_ID)
    public Result<?> updateInfo(@RequestBody @Valid UserUpdateParams params) {
        userCenterService.updateInfo(params);
        return Result.success();
    }

    @GetMapping("invitationList")
    public Result<?> invitationList(Page<InvitationCodeVO> page) {
        invitationCodeService.getInvitationList(page);
        return Result.success(page);
    }

}
